<?php
/**
 * Created by PhpStorm.
 * User: Lee
 * Date: 2019/12/19
 * Time: 16:15
 */
namespace App\Common\Utils;

use Illuminate\Http\Request;
use Illuminate\Support\Facades\Validator;

abstract class BaseRequest extends Request {
    protected $_errMsg = '';

    public function __construct()
    {
        parent::__construct();
    }

    abstract function rules();

    abstract function messages();

    public function isValid(){
        $requestData = $this->getData();

        $rules = $this->rules();
        $messages = $this->messages();

        $validator = Validator::make($requestData, $rules, $messages);
        if ($validator->fails()) {
            $errors = $validator->errors()->all();
            $this->_errMsg = $errors[0];
            return FALSE;
        }else{
            return TRUE;
        }
    }

    public function getData(){
        $requestData = $this->getContent();
        return json_decode($requestData, TRUE );
    }

    public function getErrMsg(){
        return $this->_errMsg;
    }
}
